1 //-------------------------------------------------------------------------------------------------
3 // Copyright (c) Microsoft Corporation. All rights reserved.
5 // Common heap management infrastructure for the VB products
7 //-------------------------------------------------------------------------------------------------
12 //-------------------------------------------------------------------------------------------------
14 // This is the common heap used for tracking VB memory usage.
16 //-------------------------------------------------------------------------------------------------
18 extern HANDLE g_vbCommonHeap
;
22 #define VBHeapCreate(opts) VsDebHeapCreate((opts), "VB Compiler heap");
23 #define VBHeapCreateNamed(opts,name) VsDebHeapCreate((opts), name);
24 #define VBHeapDestroy(heap) VsDebHeapDestroy((heap), true);
25 #define VBAlloc(cb) VsDebugAllocInternal(g_vbCommonHeap, HEAP_ZERO_MEMORY | HEAP_GENERATE_EXCEPTIONS, (cb), __FILE__, __LINE__, INSTANCE_GLOBAL, NULL)
26 #define VBAllocOpt(cb,opts) VsDebugAllocInternal(g_vbCommonHeap, (opts), (cb), __FILE__, __LINE__, INSTANCE_GLOBAL, NULL)
27 #define VBRealloc(pvOld, cbNew) VBAllocator::ReallocInternal(g_vbCommonHeap, (pvOld), HEAP_GENERATE_EXCEPTIONS, (cbNew), __FILE__, __LINE__, INSTANCE_GLOBAL, NULL)
28 #define VBFree(pv) VsDebugFreeInternal(g_vbCommonHeap, (pv))
29 #define VBSize(pv) VsDebugSizeInternal(g_vbCommonHeap, (pv))
31 //-------------------------------------------------------------------------------------------------
33 // Ensure that we also track allocations onto the other approved heaps
36 #define CoTaskMemAlloc(cb) VsDebOleAlloc(cb)
37 #define CoTaskMemRealloc(pv, cb) VsDebOleRealloc(pv, cb)
38 #define CoTaskMemFree(pv) VsDebOleFree(pv)
40 #define SysAllocString(str) VsDebSysAllocString((str))
41 #define SysAllocStringByteLen(str, cb) VsDebSysAllocStringByteLen((str), (cb))
42 #define SysAllocStringLen(str, cch) VsDebSysAllocStringLen((str), (cch))
47 #define VBHeapCreate(opts) HeapCreate((opts), 0, 0)
48 #define VBHeapCreateNamed(opts,name) HeapCreate((opts), 0, 0)
49 #define VBHeapDestroy(heap) HeapDestroy((heap))
50 #define VBAlloc(cb) HeapAlloc(g_vbCommonHeap, HEAP_ZERO_MEMORY | HEAP_GENERATE_EXCEPTIONS, (cb))
51 #define VBAllocOpt(cb,opts) HeapAlloc(g_vbCommonHeap, (opts), (cb))
52 #define VBRealloc(pvOld, cbNew) VBAllocator::ReallocInternal(g_vbCommonHeap, (pvOld), HEAP_GENERATE_EXCEPTIONS, (cbNew))
53 #define VBFree(pv) HeapFree(g_vbCommonHeap, 0, (pv))
54 #define VBSize(pv) HeapSize(g_vbCommonHeap, 0, (pv))
59 //-------------------------------------------------------------------------------------------------
61 // Used to force memory to be zero'd out for new
63 //-------------------------------------------------------------------------------------------------
70 extern const zeromemory_t zeromemory
;
71 void* _cdecl
operator new(size_t cbSize
, const zeromemory_t
&);
72 void* _cdecl
operator new[](size_t cbSize
, const zeromemory_t
&);
73 void* _cdecl
operator new(size_t cbSize
, NorlsAllocator
&norls
);
77 // In DEBUG mode if you said you are safe in the presence of non-zero memory then
78 // you will have a chance to prove yourself. Note we must also override operator
79 // delete here because we are allocating off of the VBHeap regardless of where
80 // we are hosted. Not doing so will cause allocs on VBHeap and frees on the process
82 #define NEW_CTOR_SAFE() \
83 void* operator new (size_t cbSize) { return VBAllocOpt(cbSize, HEAP_GENERATE_EXCEPTIONS);} \
84 void* operator new[] (size_t cbSize) { return VBAllocOpt(cbSize, HEAP_GENERATE_EXCEPTIONS); } \
85 void* operator new (size_t cbSize, const std::nothrow_t& ) { return VBAllocOpt(cbSize, 0);} \
86 void* operator new[] (size_t cbSize, const std::nothrow_t& ) { return VBAllocOpt(cbSize, 0); } \
87 void* operator new (size_t cbSize, NorlsAllocator &norls) { return norls.Alloc(cbSize); } \
88 void* operator new (size_t cbSize, const zeromemory_t& ) { return VBAlloc(cbSize); } \
89 void* operator new (size_t, void *pWhere) { return pWhere; } \
90 void operator delete(void *pv) { ::VBFree(pv); } \
91 void operator delete[](void *pv) { ::VBFree(pv); }
93 // No need to override operator delete here because new(zeromemory) will always use
94 // global new under the hood in a safe fashion.
95 #define NEW_MUST_ZERO() \
97 void* operator new (size_t cbsize); \
99 void* operator new (size_t cbsize, const zeromemory_t&) \
101 return ::operator new(cbsize, zeromemory); \
104 #define NEW_MUST_ZERO()
105 #define NEW_CTOR_SAFE()
108 //-------------------------------------------------------------------------------------------------
110 // If you get a linker error the following function please read the following.
112 // BasicCompiler.lib is linked into several VSA projects. When linking we inherit their version
113 // of new which does NOT guarantee that memory is zero'd. Instead we get whatever garbage is
114 // returned by the standard new.
116 // To guard against this the below function is defined for DEBUG builds and catches all cases where
117 // we bind to the default operator new. If you hit this then look at your change list for anywhere
118 // you added a call that includes new. There are 2 ways to fix this problem to guarantee our code
119 // will run correctly in the VSA environment
121 // 1) Use the zeromemory version of new which guarantees zero'd memory in any circumstance
122 // Foo *f = new (zeromemory) Foo();
123 // 2) If the class in question is one you own and it has constructors which guarantee it will behave
124 // properly in the presence of new then add the following macro inside your class definition
127 // In addition if you know for sure your class must have zero memory to work define NEW_MUST_ZERO
129 //-------------------------------------------------------------------------------------------------
130 #define ENFORCE_ZERO_MEMORY() \
131 void* FunctionDoesNotExist(); \
132 static inline void* __cdecl operator new(size_t sizeParam) \
133 { return FunctionDoesNotExist(); } \
134 static inline void* __cdecl operator new[](size_t sizeParam) \
135 { return FunctionDoesNotExist(); } \
136 static inline void* __cdecl operator new(size_t sizeParam, const std::nothrow_t&) \
137 { return FunctionDoesNotExist(); } \
138 static inline void* __cdecl operator new[](size_t sizeParam, const std::nothrow_t&) \
139 { return FunctionDoesNotExist(); }